<?php

namespace App\Http\Controllers\QH_Api;

use App\Http\Controllers\TK_Api\ApiBaseController;
use App\Http\Requests\QH\QHGroupBuyJoinRequest;
use App\Http\Requests\QH\QHGroupBuyStoreRequest;
use App\Models\GroupBuy;
use App\Models\GroupBuyData;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class GroupBuyController extends ApiBaseController
{

    /**
     * 创建拼团
     * @param QHGroupBuyStoreRequest $request
     * @return JsonResponse|object
     * @throws Exception
     */
    public function store(QHGroupBuyStoreRequest $request)
    {
        $params = $request->validated();

        $groupBuy = GroupBuy::find($params['groupBuyId']);
        if (!$groupBuy) throw new Exception('活动已下线');

        $groupBuyDataList = GroupBuyData::where('group_buy_id', $params['groupBuyId'])->get();
        foreach ($groupBuyDataList as $buyDatum) {
            if ($buyDatum->leader_id == $this->user->id) throw new Exception('您已开团');

            $_memberIds = $buyDatum->member_ids ?? [];
            if (in_array($this->user->id, $_memberIds)) throw new Exception('您已参团');
        }

        $data = GroupBuyData::create([
            'type' => $groupBuy->type,
            'group_buy_id' => $groupBuy->groupBuy_id,
            'data_id' => $groupBuy->data_id,
            'original_price' => $groupBuy->original_price,
            'leader_price' => $groupBuy->leader_price,
            'member_num' => $groupBuy->member_num,
            'effective_type' => $groupBuy->effective_type,
            'effective_days' => $groupBuy->effective_days,
            'effective_hours' => $groupBuy->effective_hours,
            'effective_date' => $groupBuy->effective_date,
            'start_at' => $groupBuy->effective_date,
            'end_at' => $groupBuy->effective_date,
            'status' => 1,
            'member_count' => 1,
            'member_price' => $groupBuy->member_price,
            'member_ids' => json_encode([$this->user->id]),
            'data_id' => $groupBuy->data_id,
            'data_id' => $groupBuy->data_id,
            'leader_id' => $this->user->id,
            'leader_nickname' => $this->user->nickname,
            'leader_avatar' => $this->user->avatar,
        ]);

        $groupBuyMemberIds = $groupBuy->member_ids ?? [];
        $groupBuyMemberIds[] = $this->user->id;
        $groupBuy->member_ids = $groupBuyMemberIds;
        $groupBuy->save();

        return $this->restful($data);
    }

    /**
     * 参团
     * @param QHGroupBuyJoinRequest $request
     * @return JsonResponse|object
     * @throws Exception
     */
    public function join(QHGroupBuyJoinRequest $request)
    {
        $params = $request->validated();

        $groupBuyData = GroupBuyData::find($params['groupBuyDataId']);
        if (!$groupBuyData) throw new Exception('该团不存在');

        $groupBuy = GroupBuy::find($groupBuyData->group_buy_id);
        if (!$groupBuy) throw new Exception('活动已下线');

        $groupBuyDataList = GroupBuyData::where('group_buy_id', $groupBuyData->group_buy_id)->get();
        foreach ($groupBuyDataList as $buyDatum) {
            if ($buyDatum->leader_id == $this->user->id) throw new Exception('您已开团');

            $_memberIds = $buyDatum->member_ids ?? [];
            if (in_array($this->user->id, $_memberIds)) throw new Exception('您已参团');
        }

        $groupBuyDataMemberIds = $groupBuyData->member_ids ?? [];
        $groupBuyDataMemberIds[] = $this->user->id;
        $groupBuyData->member_ids = array_filter(array_unique($groupBuyDataMemberIds));
        $groupBuyData->member_count += 1;
        $groupBuyData->save();

        $groupBuyMemberIds = $groupBuy->member_ids ?? [];
        $groupBuyMemberIds[] = $this->user->id;
        $groupBuy->member_ids = array_filter(array_unique($groupBuyMemberIds));
        $groupBuy->save();

        return $this->restful();
    }
}
